﻿#include "BrowserTableHandlerJunctionAbstract.h"
#include "WidgetBrowserTable.h"
#include "OperatorsInterface/OperAbstractJunction.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"

#include <QMenu>
namespace GUI
{
    BrowserTableHandlerJunctionAbstract::BrowserTableHandlerJunctionAbstract(WidgetBrowserTable* table) :
        BrowserTableAbstractHandler(table)
    {
        //获取连接管理器 子系统管理器
        Acoustics::FITKAcousticsDiscModel* disc = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (!disc)return;
        Acoustics::FITKAcousticsSEAModel* model = disc->getSEAModel();
        if (!model)return;
        _seaMgr = model->getSEASubSysManager();
        _junMgr = disc->getJunctionManager();
    }

    BrowserTableHandlerJunctionAbstract::~BrowserTableHandlerJunctionAbstract()
    {
    }

    void BrowserTableHandlerJunctionAbstract::contextMenu(QMenu * popMenu)
    {
        if (popMenu == nullptr || _tableWidget == nullptr) return;

        //添加按钮
        QAction* act = this->AddMenuAction(popMenu, tr("Edit"));
        connect(act, SIGNAL(triggered()), this, SLOT(editJunctionSlot()));
        //选择一个的时候，不可以批量设置属性
        act = this->AddMenuAction(popMenu, tr("Set Attribute"));
        act->setEnabled(this->enableAttriSet());
        connect(act, SIGNAL(triggered()), this, SLOT(setAttributeSlot()));
        act = this->AddMenuAction(popMenu, tr("Copy as Text"));
        connect(act, SIGNAL(triggered()), this, SLOT(copyAsTextSlot()));
        popMenu->addSeparator();
        /*******暂时移除删除********/
        //act = this->AddMenuAction(popMenu, tr("Delete"));
        //connect(act, SIGNAL(triggered()), this, SLOT(removeJunctionSlot()));
    }

    void BrowserTableHandlerJunctionAbstract::headerContextMenu(QMenu * popMenu, int coluIndex)
    {
        if (popMenu == nullptr || _tableWidget == nullptr || coluIndex < 0) return;
    }

    EventOper::OperAbstractJunction* BrowserTableHandlerJunctionAbstract::getJunctionOper()
    {
        //纯虚函数
        return nullptr;
    }

    bool BrowserTableHandlerJunctionAbstract::enableAttriSet()
    {
        return false;
    }

    void BrowserTableHandlerJunctionAbstract::editJunctionSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractJunction* oper = this->getJunctionOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
        //操作器调用
        oper->editJunction(matID);
    }

    void BrowserTableHandlerJunctionAbstract::itemDoubleClicked(int row, int colum)
    {
        //双击编辑操作
        if (_tableWidget == nullptr) return;
        this->editJunctionSlot();
    }

    void BrowserTableHandlerJunctionAbstract::removeJunctionSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractJunction* oper = this->getJunctionOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
        //操作器调用
        oper->removeJunction(matID);
    }

    void BrowserTableHandlerJunctionAbstract::setAttributeSlot()
    {
        if (_tableWidget == nullptr) return;
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2)return;
        EventOper::OperAbstractJunction* oper = this->getJunctionOper();
        if (oper == nullptr) return;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return;
        //名称列
        const int colIndex = item->column();
        QTableWidgetItem* hitem = _tableWidget->horizontalHeaderItem(colIndex);
        if (hitem == nullptr) return;
        const QString text = hitem->text();
        QList<int> itemID;
        //选取的对象根据第一个对象判断
        for (QTableWidgetItem* item : selectItems)
        {
            if (item == nullptr) continue;
            int col = item->column();
            if (col == colIndex)
                //记录对象ID
                itemID.append(item->type());
        }
        //执行批量修改
        oper->setAttribute(text, item->text(), colIndex, itemID);
    }

}